package com.healthcloud.controller.modules.patient;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.healthcloud.constants.AccompanyBlNoMedicalConstants;
import com.healthcloud.controller.modules.common.BaseController;
import com.healthcloud.dto.req.accompany.AccompanyBlFirstFromHospImportDTO;
import com.healthcloud.dto.req.accompany.AccompanyBlFirstImportDTO;
import com.healthcloud.dto.req.accompany.AccompanyBlImportDTO;
import com.healthcloud.dto.req.accompany.AccompanyBlMonth12ImportDTO;
import com.healthcloud.dto.req.accompany.AccompanyBlMonth18ImportDTO;
import com.healthcloud.dto.req.accompany.AccompanyBlMonth24ImportDTO;
import com.healthcloud.dto.req.accompany.AccompanyBlMonth6ImportDTO;
import com.healthcloud.entity.accompany.*;
import com.healthcloud.entity.common.PageList;
import com.healthcloud.entity.patient.BoneLooseDrugEntity;
import com.healthcloud.entity.patient.PatientDataEntity;
import com.healthcloud.enums.AccompanyBlHasMedicalFlagEnum;
import com.healthcloud.service.HlHospitalService;
import com.healthcloud.service.accompany.*;
import com.healthcloud.service.patient.BoneLooseDrugService;
import com.healthcloud.service.patient.PatientDataService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.DateUtils;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 病人随访
 */
@Controller
@RequestMapping("accompany")
@Slf4j
public class AccompanyController extends BaseController {

    @Autowired
    private AccompanyBpService accompanyBpService;

    @Autowired
    private AccompanyBsService accompanyBsService;

    @Autowired
    private AccompanyBfService accompanyBfService;

    @Autowired
    private AccompanyBlService accompanyBlService;

    @Autowired
    private HlHospitalService hlHospitalService;

    @Autowired
    private PatientDataService patientDataService;

    @Autowired
    private BoneLooseDrugService boneLooseDrugService;

    @Autowired
    private AccompanyMcService accompanyMcService;

    /**
     * 无药物骨松 service 组件
     */
    @Autowired
    private AccompanyBlNoMedicalService accompanyBlNoMedicalService;

    @ApiOperation(value = "血压随访病人列表")
    @RequestMapping(value = "/bpPatients", method = RequestMethod.GET)
    public String bpPatients() {
        setAttr();
        return "bus/accompany/bpPatients";
    }

    @ApiOperation(value = "血糖随访病人列表")
    @RequestMapping(value = "/bsPatients", method = RequestMethod.GET)
    public String bsPatients() {
        setAttr();
        return "bus/accompany/bsPatients";
    }

    @ApiOperation(value = "血脂随访病人列表")
    @RequestMapping(value = "/bfPatients", method = RequestMethod.GET)
    public String bfPatients() {
        setAttr();
        return "bus/accompany/bfPatients";
    }

    @ApiOperation(value = "骨松随访病人列表")
    @RequestMapping(value = "/blPatients", method = RequestMethod.GET)
    public String blPatients() {
        setAttr();
        return "bus/accompany/blPatients";
    }

    @ApiOperation(value = "血压随访列表")
    @RequestMapping(value = "/bpAccompanys", method = RequestMethod.GET)
    public String bpAccompanys() {
        setAttr();
        return "bus/accompany/bpAccompanys";
    }

    @ApiOperation(value = "血糖随访列表")
    @RequestMapping(value = "/bsAccompanys", method = RequestMethod.GET)
    public String bsAccompanys() {
        setAttr();
        return "bus/accompany/bsAccompanys";
    }

    @ApiOperation(value = "血脂随访列表")
    @RequestMapping(value = "/bfAccompanys", method = RequestMethod.GET)
    public String bfAccompanys() {
        setAttr();
        return "bus/accompany/bfAccompanys";
    }

    @ApiOperation(value = "骨松随访列表")
    @RequestMapping(value = "/blAccompanys", method = RequestMethod.GET)
    public String blAccompanys() {
        setAttr();
        return "bus/accompany/blAccompanys";
    }


    @ApiOperation(value = "骨松随访(钱湖)随访信息")
    @RequestMapping(value = "/qianhu/blAccompanys", method = RequestMethod.GET)
    public String blQHAccompanys() {
        setAttr();
        return "bus/accompany/qianhu/blAccompanys";
    }

    @ApiOperation(value = "血压查看")
    @RequestMapping(value = "/bpAccompany", method = RequestMethod.GET)
    public String bpAccompany() {
        setAttr();
        return "bus/accompany/bpAccompany";
    }

    @ApiOperation(value = "血糖查看")
    @RequestMapping(value = "/bsAccompany", method = RequestMethod.GET)
    public String bsAccompany() {
        setAttr();
        return "bus/accompany/bsAccompany";
    }

    @ApiOperation(value = "血脂查看")
    @RequestMapping(value = "/bfAccompany", method = RequestMethod.GET)
    public String bfAccompany() {
        setAttr();
        return "bus/accompany/bfAccompany";
    }

    @ApiOperation(value = "骨松查看")
    @RequestMapping(value = "/blAccompany", method = RequestMethod.GET)
    public String blAccompany() {
        setAttr();

        // 若是无药物骨松随访，则跳转无药物骨松随访详情页面
        String idStr = getAttr("id");
        if (StrUtil.isNotBlank(idStr)) {
            AccompanyBlVo blVo = accompanyBlService.queryObjectVo(Long.valueOf(idStr));
            if (AccompanyBlHasMedicalFlagEnum.NO_MEDICAL.getCode().equals(blVo.getHasMedical())) {
                return "bus/accompany/blNoMedicalAccompany";
            }
        }

        // 跳转骨松随访详情页面
        return "bus/accompany/blAccompany";
    }

    @ApiOperation(value = "骨松查看(钱湖)")
    @RequestMapping(value = "/blQHAccompany", method = RequestMethod.GET)
    public String blQHAccompany() {
        setAttr();

        // 若是无药物骨松随访，则跳转无药物骨松随访详情页面
        String idStr = getAttr("id");
        if (StrUtil.isNotBlank(idStr)) {
            AccompanyBlVo blVo = accompanyBlService.queryObjectVo(Long.valueOf(idStr));
            if (AccompanyBlHasMedicalFlagEnum.NO_MEDICAL.getCode().equals(blVo.getHasMedical())) {
                return "bus/accompany/qianhu/blNoMedicalAccompany";
            }
        }

        // 跳转骨松随访详情页面
        return "bus/accompany/blAccompany";
    }

    @ApiOperation(value = "血压提醒")
    @RequestMapping(value = "/bpReminds", method = RequestMethod.GET)
    public String bpReminds() {
        setAttr();
        return "bus/accompany/bpReminds";
    }

    @ApiOperation(value = "血糖提醒")
    @RequestMapping(value = "/bsReminds", method = RequestMethod.GET)
    public String bsReminds() {
        setAttr();
        return "bus/accompany/bsReminds";
    }

    @ApiOperation(value = "血脂提醒")
    @RequestMapping(value = "/bfReminds", method = RequestMethod.GET)
    public String bfReminds() {
        setAttr();
        return "bus/accompany/bfReminds";
    }

    @ApiOperation(value = "骨松提醒")
    @RequestMapping(value = "/blReminds", method = RequestMethod.GET)
    public String blReminds() {
        setAttr();
        return "bus/accompany/blReminds";
    }

    @ApiOperation(value = "骨松提醒(钱湖)")
    @RequestMapping(value = "/qianhu/blReminds", method = RequestMethod.GET)
    public String qianhuBlReminds() {
        setAttr();
        return "bus/accompany/qianhu/blReminds";
    }

    @ApiOperation(value = "骨松随访(钱湖)")
    @RequestMapping(value = "/qianhu/blPatients", method = RequestMethod.GET)
    public String qianhuBlPatients() {
        setAttr();
        return "bus/accompany/qianhu/blPatients";
    }

    @ApiOperation(value = "血压随访列表")
    @RequestMapping(value = "/bpList", method = RequestMethod.POST)
    @ResponseBody
    public PageList bpList(AccompanyBpVo entity, PageParam pageParam) {
        List<AccompanyBpVo> bps = accompanyBpService.queryListVo(entity, pageParam);
        Integer count = accompanyBpService.queryTotalVo(entity);
        return new PageList(bps, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "血糖随访列表")
    @RequestMapping(value = "/bsList", method = RequestMethod.POST)
    @ResponseBody
    public PageList bsList(AccompanyBsVo entity, PageParam pageParam) {
        List<AccompanyBsVo> bps = accompanyBsService.queryListVo(entity, pageParam);
        Integer count = accompanyBsService.queryTotalVo(entity);
        return new PageList(bps, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "血脂随访列表")
    @RequestMapping(value = "/bfList", method = RequestMethod.POST)
    @ResponseBody
    public PageList bfList(AccompanyBfVo entity, PageParam pageParam) {
        List<AccompanyBfVo> bpf = accompanyBfService.queryListVo(entity, pageParam);
        Integer count = accompanyBfService.queryTotalVo(entity);
        return new PageList(bpf, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "骨松随访列表")
    @RequestMapping(value = "/blList", method = RequestMethod.POST)
    @ResponseBody
    public PageList blList(AccompanyBlVo entity, PageParam pageParam) {
        List<AccompanyBlVo> blf = accompanyBlService.queryListVo(entity, pageParam);
        Integer count = accompanyBlService.queryTotalVo(entity);
        return new PageList(blf, count, count, pageParam.getsEcho());
    }

    /*待修改*/
    /*@ApiOperation(value = "骨松随访(钱湖)列表")
    @RequestMapping(value = "/blQHList", method = RequestMethod.POST)
    @ResponseBody
    public PageList blQHList(AccompanyBlVo entity, PageParam pageParam) {
        List<AccompanyBlVo> blf = accompanyBlService.queryListVo(entity, pageParam);
        Integer count = accompanyBlService.queryTotalVo(entity);
        return new PageList(blf, count, count, pageParam.getsEcho());
    }*/

    @ApiOperation(value = "根据身份证号，查询上一次有药物骨松随访的 id")
    @RequestMapping(value = "/getLastBlIdWithMedical", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes getLastBlIdWithMedical(String idCard) {
        if (StrUtil.isEmptyIfStr(idCard)) {
            return ApiRes.failure("参数有误，身份证号不能为空。");
        }
        Integer id = accompanyBlService.getLastBlIdWithMedical(idCard);
        Map<String, Integer> idMap = Maps.newHashMap();
        idMap.put("lastCheckId", id);

        return null != id ? ApiRes.success(idMap) : ApiRes.failure();
    }

    @ApiOperation(value = "血压随访结果")
    @RequestMapping(value = "/bpData", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes bpData(Long id) {
        AccompanyBpVo entity = accompanyBpService.queryObjectVo(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "血糖随访结果")
    @RequestMapping(value = "/bsData", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes bsData(Long id) {
        AccompanyBsVo entity = accompanyBsService.queryObjectVo(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "血脂随访结果")
    @RequestMapping(value = "/bfData", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes bfData(Long id) {
        AccompanyBfVo entity = accompanyBfService.queryObjectVo(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "骨松随访结果")
    @RequestMapping(value = "/blData", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes blData(Long id) {
        AccompanyBlVo entity = accompanyBlService.queryObjectVo(id);
        return new ApiRes(entity);
    }

    @ApiOperation(value = "保存血压随访")
    @RequestMapping(value = "/saveBp", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveBp(AccompanyBpEntity entity) {
        accompanyBpService.saveOrUpdate(entity);

        /**
         * 病人最后一次数据回显
         */
        PatientDataEntity data = new PatientDataEntity();
        data.setIdCard(entity.getIdCard());
        data.setHighPressure(entity.getHighPressure());
        data.setLowPressure(entity.getLowPressure());
        data.setHeight(entity.getHeight());
        data.setWeight(entity.getWeight());
        data.setBmi(entity.getBmi());
        data.setWaist(entity.getWaist());
        data.setHeartRate(entity.getHeartRate());
        data.setUpdateTime(new Date());
        patientDataService.saveOrUpdate(data);
        return ApiRes.SUCCESS;
    }

    @ApiIgnore(value = "慈溪血压随访保存")
    @RequestMapping(value = "/cx/n/saveBp", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes cxSaveBp(AccompanyBpEntity entity) {
        entity.setSource(2);
        accompanyBpService.save(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存血糖随访")
    @RequestMapping(value = "/saveBs", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveBs(AccompanyBsEntity entity) {
        accompanyBsService.saveOrUpdate(entity);

        /**
         * 病人最后一次数据回显
         */
        PatientDataEntity data = new PatientDataEntity();
        data.setIdCard(entity.getIdCard());
        data.setHighPressure(entity.getHighPressure());
        data.setLowPressure(entity.getLowPressure());
        data.setHeight(entity.getHeight());
        data.setWeight(entity.getWeight());
        data.setBmi(entity.getBmi());
        data.setWaist(entity.getWaistline());
        data.setUpdateTime(new Date());
        patientDataService.saveOrUpdate(data);
        return ApiRes.SUCCESS;
    }

    @ApiIgnore(value = "慈溪保存血糖随访")
    @RequestMapping(value = "/cx/n/saveBs", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes cxSaveBs(AccompanyBsEntity entity) {
        entity.setSource(2);
        accompanyBsService.save(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存血脂随访")
    @RequestMapping(value = "/saveBf", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveBf(AccompanyBfEntity entity) {
        accompanyBfService.saveOrUpdate(entity);

        /**
         * 病人最后一次数据回显
         */
        PatientDataEntity data = new PatientDataEntity();
        data.setIdCard(entity.getIdCard());
        data.setBloodFat(entity.getBloodFat());
        data.setTriglyceride(entity.getTriglyceride());
        data.setCholesterol(entity.getCholesterol());
        data.setLowCholesterol(entity.getLowCholesterol());
        data.setHighCholesterol(entity.getHighCholesterol());
        patientDataService.saveOrUpdate(data);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "保存骨松随访")
    @RequestMapping(value = "/saveBl", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes saveBl(@RequestBody AccompanyBlDto entity) {
        accompanyBlService.saveOrUpdate(entity);
        boneLooseDrugService.deleteByCheckId(entity.getId());
        List<BoneLooseDrugEntity> drugs = entity.getDrugs();
        if (drugs != null && drugs.size() > 0) {
            for (BoneLooseDrugEntity drug : drugs) {
                drug.setIdCard(entity.getIdCard());
                drug.setCheckId(entity.getId());
                boneLooseDrugService.save(drug);
            }
        }
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除血压随访")
    @RequestMapping(value = "/deleteBp", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteBp(Long[] ids) {
        accompanyBpService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除血糖随访")
    @RequestMapping(value = "/deleteBs", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteBs(Long[] ids) {
        accompanyBsService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除血脂随访")
    @RequestMapping(value = "/deleteBf", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteBf(Long[] ids) {
        accompanyBfService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除骨松随访")
    @RequestMapping(value = "/deleteBl", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteBl(Long[] ids) {
        accompanyBlService.deleteBatch(ids);
        // 逻辑删除无药物骨松随访
        accompanyBlNoMedicalService.batchRemoveByAccompanyBlIds(ids);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "血压随访提醒列表")
    @RequestMapping(value = "/bpRemindList", method = RequestMethod.POST)
    @ResponseBody
    public PageList bpRemindList(AccompanyBpVo entity, PageParam pageParam) {
        String ids = hlHospitalService.getChildeIds();
        entity.setHospitalIds(ids);
        List rs = accompanyBpService.queryRemindList(entity, pageParam);
        Integer count = accompanyBpService.queryRemindTotal(entity);
        return new PageList(rs, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "血糖随访提醒列表")
    @RequestMapping(value = "/bsRemindList", method = RequestMethod.POST)
    @ResponseBody
    public PageList bsRemindList(AccompanyBsVo entity, PageParam pageParam) {
        String ids = hlHospitalService.getChildeIds();
        entity.setHospitalIds(ids);
        List rs = accompanyBsService.queryRemindList(entity, pageParam);
        Integer count = accompanyBsService.queryRemindTotal(entity);
        return new PageList(rs, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "血脂随访提醒列表")
    @RequestMapping(value = "/bfRemindList", method = RequestMethod.POST)
    @ResponseBody
    public PageList bfRemindList(AccompanyBfVo entity, PageParam pageParam) {
        String ids = hlHospitalService.getChildeIds();
        entity.setHospitalIds(ids);
        List rs = accompanyBfService.queryRemindList(entity, pageParam);
        Integer count = accompanyBfService.queryRemindTotal(entity);
        return new PageList(rs, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "骨松随访提醒列表")
    @RequestMapping(value = "/blRemindList", method = RequestMethod.POST)
    @ResponseBody
    public PageList blRemindList(AccompanyBlVo entity, PageParam pageParam) {
        String ids = hlHospitalService.getChildeIds();
        entity.setHospitalIds(ids);
        // 获取当前时间往前推两年的时间
        Date previousStartOfYear = DateUtils.getPreviousStartOfYear(new Date(), 2);
        entity.setFilterStartTime(previousStartOfYear);
        List<Map<String, Object>> rs = accompanyBlService.queryRemindList(entity, pageParam);
        for (Map<String, Object> r : rs) {
            Date createdTime = (Date)r.get("createdTime");
            Date accompany_time = (Date)r.get("accompany_time");
            // 判断createdTime和accompany_time是否大于两年
            if (createdTime != null && accompany_time != null) {
                long diff = accompany_time.getTime() - createdTime.getTime();
                long diffYear = diff / (24 * 60 * 60 * 1000 * 365);
                if (diffYear >= 2) {
                    r.put("isOverTwoYear", 1);
                } else {
                    r.put("isOverTwoYear", 2);
                }
            }
        }
        Integer count = accompanyBlService.queryRemindTotal(entity);
        return new PageList(rs, count, count, pageParam.getsEcho());
    }


    // ------------------------------- 无药物、区分时间段 骨松随访 start -----------------------------------------

    /**
     * 导入excel数据注意事项：
     * 患者手机号：骨松筛查及首次问卷，6、12、18、24个月的excel中，都有 patientPhone（作为患者匹配随访数据的依据）
     * 患者身份证号：骨松筛查及首次问卷，有 idCard；6、12、18、24个月的随访数据，没有 idCard
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation("导入骨松随访（没有用药、区分时间段）excel 数据")
    @PostMapping("/importBLNoMedicalData")
    @ResponseBody
    public ApiRes importData(@RequestParam MultipartFile file) {
        try {
            long importStartTime = System.currentTimeMillis();

            /// 解析 excel 文件，获取第一个 sheet 名称
            ExcelUtil<AccompanyBlImportDTO> excelUtil = new ExcelUtil<>(AccompanyBlImportDTO.class);
            String sheetName = excelUtil.getFirstSheetName(file.getInputStream());
            if (StrUtil.isBlank(sheetName)) {
                return ApiRes.failure("excel sheet 名称为空，文件不正确。");
            }

            /// 根据 sheet 名称，确定 excel 解析类，解析并新增数据到 db 中
            ApiRes handleRes = null;
            if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_FIRST.equals(sheetName)) {
                // 骨松筛查及首次问卷
                ExcelUtil<AccompanyBlFirstImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlFirstImportDTO.class);
                List<AccompanyBlFirstImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelFirstData(list);
            } else if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_MONTH_6.equals(sheetName)) {
                // 6个月随访问卷
                ExcelUtil<AccompanyBlMonth6ImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlMonth6ImportDTO.class);
                List<AccompanyBlMonth6ImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelMonth6Data(list);
            } else if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_MONTH_12.equals(sheetName)) {
                // 12个月随访问卷
                ExcelUtil<AccompanyBlMonth12ImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlMonth12ImportDTO.class);
                List<AccompanyBlMonth12ImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelMonth12Data(list);
            } else if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_MONTH_18.equals(sheetName)) {
                // 18个月随访问卷
                ExcelUtil<AccompanyBlMonth18ImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlMonth18ImportDTO.class);
                List<AccompanyBlMonth18ImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelMonth18Data(list);
            } else if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_MONTH_24.equals(sheetName)) {
                // 24个月随访问卷
                ExcelUtil<AccompanyBlMonth24ImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlMonth24ImportDTO.class);
                List<AccompanyBlMonth24ImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelMonth24Data(list);
            } else {
                handleRes = ApiRes.failure("请重新核对文件是否正确。");
            }

            long importEndTime = System.currentTimeMillis();
            log.info("导入骨松随访（没有用药、区分时间段）excel 数据，结果=[{}]，耗时: [{}]ms",
                    ApiRes.SUCCESS_STATUS.equals(handleRes.getCode()) ? "导入成功" : handleRes.getMsg(),
                    (importEndTime - importStartTime));
            return ApiRes.returnWrapperApiRes(handleRes);
        } catch (Exception e) {
            log.error("导入骨松随访（没有用药、区分时间段）excel 数据出现异常：[{}]", e.getMessage());
            return ApiRes.failure("导入失败。未知错误。" + e.getMessage());
        }
    }

    /**
     * 导入excel数据注意事项：
     * 患者手机号：骨松筛查及首次问卷，6、12、18、24个月的excel中，都有 patientPhone（作为患者匹配随访数据的依据）
     * 患者身份证号：骨松筛查及首次问卷，有 idCard；6、12、18、24个月的随访数据，没有 idCard
     */
    @ApiOperation("导入骨松随访（没有用药、区分时间段）excel 数据（2020-09-21 按从医院处获取的 excel 模板（部分需要修改））")
    @PostMapping("/importBLNoMedicalDataForHosp")
    @ResponseBody
    public ApiRes importDataForHosp(@RequestParam MultipartFile file) {
        try {
            long importStartTime = System.currentTimeMillis();

            /// 解析 excel 文件，获取第一个 sheet 名称
            ExcelUtil<AccompanyBlImportDTO> excelUtil = new ExcelUtil<>(AccompanyBlImportDTO.class);
            String sheetName = excelUtil.getFirstSheetName(file.getInputStream());
            if (StrUtil.isBlank(sheetName)) {
                return ApiRes.failure("excel sheet 名称为空，文件不正确。");
            }

            /// 根据 sheet 名称，确定 excel 解析类，解析并新增数据到 db 中
            ApiRes handleRes = null;
            if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_FIRST.equals(sheetName)) {
                // 骨松筛查及首次问卷
                ExcelUtil<AccompanyBlFirstFromHospImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlFirstFromHospImportDTO.class);
                List<AccompanyBlFirstFromHospImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelFirstDataForHosp(list);
            }
            /* else if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_MONTH_6.equals(sheetName)) {
                // 6个月随访问卷
                ExcelUtil<AccompanyBlMonth6ImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlMonth6ImportDTO.class);
                List<AccompanyBlMonth6ImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelMonth6Data(list);
            } else if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_MONTH_12.equals(sheetName)) {
                // 12个月随访问卷
                ExcelUtil<AccompanyBlMonth12ImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlMonth12ImportDTO.class);
                List<AccompanyBlMonth12ImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelMonth12Data(list);
            } else if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_MONTH_18.equals(sheetName)) {
                // 18个月随访问卷
                ExcelUtil<AccompanyBlMonth18ImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlMonth18ImportDTO.class);
                List<AccompanyBlMonth18ImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelMonth18Data(list);
            } else if (AccompanyBlNoMedicalConstants.EXCEL_SHEET_MONTH_24.equals(sheetName)) {
                // 24个月随访问卷
                ExcelUtil<AccompanyBlMonth24ImportDTO> finalExcelUtil = new ExcelUtil<>(AccompanyBlMonth24ImportDTO.class);
                List<AccompanyBlMonth24ImportDTO> list = finalExcelUtil.importMatchExcel(file.getInputStream());
                handleRes = accompanyBlNoMedicalService.importExcelMonth24Data(list);
            } */
            else {
                handleRes = ApiRes.failure("请重新核对文件是否正确。");
            }

            long importEndTime = System.currentTimeMillis();
            log.info("导入骨松随访（没有用药、区分时间段）excel 数据，结果=[{}]，耗时: [{}]ms",
                    ApiRes.SUCCESS_STATUS.equals(handleRes.getCode()) ? "导入成功" : handleRes.getMsg(),
                    (importEndTime - importStartTime));
            return ApiRes.returnWrapperApiRes(handleRes);
        } catch (Exception e) {
            log.error("导入骨松随访（没有用药、区分时间段）excel 数据出现异常：[{}]", e.getMessage());
            return ApiRes.failure("导入失败。未知错误。" + e.getMessage());
        }
    }

    @ApiOperation(value = "无药物骨松随访查看")
    @GetMapping(value = "/blNoMedicalAccompany")
    public String blNoMedicalAccompany() {
        setAttr();
        return "bus/accompany/blNoMedicalAccompany";
    }

    @ApiOperation(value = "保存无药物骨松随访（新增/修改）")
    @PostMapping(value = "/saveNoMedicalBl")
    @ResponseBody
    public ApiRes saveBl(@RequestBody AccompanyBlNoMedicalEntity entity) {
        ApiRes res = accompanyBlNoMedicalService.saveOrUpdate(entity);
        return ApiRes.SUCCESS_STATUS.equals(res.getCode())
                ? ApiRes.SUCCESS : ApiRes.failure(res.getMsg());
    }

    /**
     * accompanyBlId 是 accompany_bl 的 id
     */
    @ApiOperation(value = "无药物骨松随访结果")
    @RequestMapping(value = "/blNoMedicalData", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes blNoMedicalData(Long accompanyBlId) {
        AccompanyBlNoMedicalEntity res = accompanyBlNoMedicalService.queryByAccompanyBlId(accompanyBlId);
        return new ApiRes(res);
    }

    // ------------------------------- 无药物、区分时间段 骨松随访 end -----------------------------------------


    // ------------------------------- 医疗服务板块 -----------------------------------------
    @ApiOperation(value = "医疗服务（新增/修改）")
    @PostMapping(value = "/saveMedicalService")
    @ResponseBody
    public ApiRes savemc(@RequestBody AccompanyMcEntity entity) {
        ApiRes res = accompanyMcService.saveOrUpdateMc(entity);
        return ApiRes.SUCCESS_STATUS.equals(res.getCode())
                ? ApiRes.SUCCESS : ApiRes.failure(res.getMsg());
    }

    @ApiOperation(value = "医疗服务列表")
    @RequestMapping(value = "/medicalService", method = RequestMethod.POST)
    @ResponseBody
    public PageList queryList(AccompanyMcEntity entity, PageParam pageParam) {
        List<AccompanyMcEntity> blf = accompanyMcService.queryList(entity, pageParam);
        Integer count = accompanyMcService.queryTotalVo(entity);
        return new PageList(blf, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "医疗服务列表")
    @RequestMapping(value = "/medicalServiceById", method = RequestMethod.POST)
    @ResponseBody
    public PageList medicalServiceById(AccompanyMcEntity entity, PageParam pageParam) {
        List<AccompanyMcEntity> blf = accompanyMcService.medicalServiceById(entity, pageParam);
        Integer count = 1;
        return new PageList(blf, count, count, pageParam.getsEcho());
    }


    @ApiOperation(value = "骨松查看")
    @RequestMapping(value = "/blAccompanyService", method = RequestMethod.GET)
    public String blAccompanyService() {
        setAttr();
        String idStr = getAttr("id");
        AccompanyMcEntity blf = accompanyMcService.queryById(idStr);
        // 跳转骨松随访详情页面
        return "bus/accompany/blAccompanyService";
    }
}